package com.wing.cabinet.service;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wing.cabinet.dao.CabinetCargoLaneDao;
import com.wing.cabinet.model.entity.CabinetCargoLane;
import com.wing.cabinet.model.entity.StorageInventoryInout;
import com.wing.cabinet.model.request.CabinetCargoLaneForm;
import com.wing.cabinet.model.request.UseInventoryInoutDetailForm;
import com.wing.cabinet.model.response.CabinetCargoLaneVO;
import com.wing.cabinet.model.response.CabinetCargoSpaceVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 刀柜货道-ServiceImpl
 *
 * @author
 */
@Service
@Slf4j
public class CabinetCargoLaneServiceImpl extends ServiceImpl<CabinetCargoLaneDao, CabinetCargoLane> implements CabinetCargoLaneService {

    @Override
    public int add(CabinetCargoLaneForm cabinetCargoLaneForm) {

        //校验

        CabinetCargoLane cabinetCargoLane =new CabinetCargoLane();
        CopyOptions copyOptions = CopyOptions.create()
        .setEditable(CabinetCargoLane.class)
        .setIgnoreError(true)
        .setIgnoreNullValue(true);
        BeanUtil.copyProperties(cabinetCargoLaneForm, cabinetCargoLane, copyOptions);

        return baseMapper.insert(cabinetCargoLane);
    }

    @Override
    public List<CabinetCargoLaneVO> list4app(Map<String, Object> params) {
        return baseMapper.list4app(params);
    }

    @Override
    public Page<CabinetCargoLaneVO> pageList4app(Page page, Map<String, Object> params) {
        List<CabinetCargoLaneVO> list = baseMapper.list4app(page, params);
        page.setRecords(list);
        return page;
    }

    @Override
    public Page<CabinetCargoLaneVO> pageList4Check(Page page, Map<String, Object> params) {
        List<CabinetCargoLaneVO> list = baseMapper.list4Check(page, params);
        page.setRecords(list);
        return page;
    }

    @Override
    public CabinetCargoLaneVO get4appById(Long id) {
        return baseMapper.get4appById(id);
    }

    @Override
    public List<CabinetCargoLaneVO> getUsableSpaces(String cabCode,String skuCode,Integer quantity) {
        List<CabinetCargoLaneVO> resultList=new ArrayList<>();
        List<CabinetCargoLaneVO> spaceVOS= baseMapper.getUsableSpace(cabCode,skuCode,quantity);
        if (spaceVOS.size()>0){
            resultList.addAll(spaceVOS);
        }
        List<CabinetCargoLaneVO> emptySpaces= baseMapper.getEmptySpaces(cabCode);
        if (emptySpaces.size()>0){
            resultList.addAll(emptySpaces);
        }
        return resultList;
    }

    public List<CabinetCargoLaneVO> getSkuSpaces(String cabCode) {
        return baseMapper.getSkuSpaces(cabCode);
    }

    @Override
    public List<CabinetCargoLaneVO> getWcabSpaceByCabCode(String cabCode,List<Integer> spaceTypes) {
        String[] cabCodes=cabCode.split(",");
        return baseMapper.getWcabSpaceByCabCode(cabCodes,spaceTypes);
    }

    @Override
    public List<CabinetCargoLaneVO> getWcabOptimalSpaceByCabCode(String cabCode,List<Integer> spaceTypes,String skuCode,String orderDesc) {
        String[] cabCodes=cabCode.split(",");
        return baseMapper.getWcabOptimalSpaceByCabCode(cabCodes,spaceTypes,skuCode,orderDesc);
    }

    @Override
    public List<CabinetCargoLaneVO> getWcabEmptySpaces(String cabCode) {
        String[] cabCodes=cabCode.split(",");
        return baseMapper.getWcabEmptySpaces(cabCodes);
    }

    @Override
    public Integer countLanesByMainCabCode(String mainCabCode) {
        return baseMapper.countLanesByMainCabCode(mainCabCode);
    }
}
